package com.itcast.rpc.core.codec;

import com.itcast.rpc.core.base.BaseRpcBean;
import com.itcast.rpc.core.util.HessianSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @program: bio_tcp
 * @description: 编码器编码
 * @author: zhanghz001
 * @create: 2021-02-23 14:11
 **/
public class RpcEncoder extends MessageToByteEncoder<BaseRpcBean> {
    private HessianSerializer hessianSerializer = new HessianSerializer();
    
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext,
                          BaseRpcBean baseRpcBean, ByteBuf byteBuf) throws Exception {
        byte[] bytes = hessianSerializer.serialize(baseRpcBean);
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);
    }
}
